package ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ssm.pojo.UserStudent;
import ssm.service.UserStudentService;
import ssm.util.JSONResult;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/student")
public class UserStudentController {

    @Autowired
    private UserStudentService userStudentService;

    @RequestMapping("/addUserStudent")
    public ModelAndView toaddUserStudent(){
        ModelAndView modelAndView = new ModelAndView("addUserStudent");
        return modelAndView;
    }


    @RequestMapping("/listUserStudent")
    public ModelAndView tolistUserStudent(){
        ModelAndView modelAndView = new ModelAndView("listUserStudent");
        return modelAndView;
    }

    @RequestMapping("/updateUserStudent")
    public ModelAndView toUpdateUserStudent(){
        ModelAndView modelAndView = new ModelAndView("updateUserStudent");
        return  modelAndView;
    }

    @RequestMapping(value="/add",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult addUserStudent(HttpServletRequest request){
        JSONResult result;
        UserStudent userStudent = getUserStudent(request);
        System.out.println("userStudent"+userStudent);
        System.out.println("添加学生");
        if (userStudentService.insertStudent(userStudent)){
            result = new JSONResult(userStudent);
            System.out.println("添加成功");
            return result;
        }else{
            result = new JSONResult();
            System.out.println("false");
            return result;
        }
    }

    @RequestMapping(value="/list",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult listUserStudent(){
        System.out.println("list层");
        List<UserStudent> list = userStudentService.selectStudentAll();
        System.out.println("list完成");
        for (int i = 0; i < list.size(); i++) {
            UserStudent userStudent = list.get(i);
            System.out.println(userStudent.getStudentId()+" "+userStudent.getStudentName()
            +" "+userStudent.getStudentClass());
        }
        return new JSONResult(list);
    }


    @RequestMapping(value="/find",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult findUserStudent(HttpServletRequest request){
        JSONResult result;
        System.out.println("find开始");
        Integer studentId = Integer.parseInt(request.getParameter("studentId"));
        UserStudent userStudent = userStudentService.selectStudentOne(studentId);
        System.out.println("find完成");
        result = new JSONResult(userStudent);
        result.setMessage("success");
        return result;
    }


    @RequestMapping(value="/delete",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult deleteUserStudent(HttpServletRequest request){
        JSONResult result = new JSONResult();
        Integer studentId = Integer.parseInt(request.getParameter("studentId"));
        if(!userStudentService.deleteStudent(studentId)){
            result.setMessage("error");
        }
        return result;
    }


    @RequestMapping(value="/update",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult updateUserStudent(HttpServletRequest request) {
        JSONResult result ;
        Integer studentId = Integer.parseInt(request.getParameter("studentId"));
        UserStudent userStudent = getUserStudent(request);
        userStudent.setStudentId(studentId);
        if (!userStudentService.updateStudent(userStudent)) {
            result = new JSONResult(userStudent);
            System.out.println("修成功controller");
            return result;
        } else {
            result = new JSONResult();
            return result;
        }
    }


    public UserStudent getUserStudent(HttpServletRequest request){
        UserStudent userStudent = new UserStudent();
        Integer studentId = Integer.parseInt(request.getParameter("studentId"));
        String studentName = request.getParameter("studentName");
        Integer studentClass = Integer.parseInt(request.getParameter("studentClass"));
        userStudent.setStudentId(studentId);
        userStudent.setStudentName(studentName);
        userStudent.setStudentClass(studentClass);
        return userStudent;
    }



}
